<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once(APPPATH . '/controllers/super_admin' . EXT);
class Group extends Super_admin {

    function __construct() {
        parent::__construct();

        $this->load->model(array(
            'Group_model',
            'User_model',
            'User_group_model',
        ));

        $this->load->helper(array(
            'security',
            'string',
        ));
    }

    function index() {
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller

        $this->list_records();
    }

    /**
     * list_records
     *
     * Load information list
     *
     * @access public
     * @param void
     * @return void
     */
    function list_records() {
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller

        $this->load->view('group/list');
    }
    /**
     * members
     *
     * Load information list
     *
     * @access public
     * @param void
     * @return void
     */
    function members($group_id) {
        $access = array('instructor');
        $this->_is_authorize($access); //check super_admin controller

        $data['group'] =  $this->Group_model->get_record(array('where' => 'group_id = "'.$group_id.'"'));

        $this->load->view('group/members', $data);
    }
    /**
     * student_list_records
     *
     * Load information list for student
     *
     * @access public
     * @param void
     * @return void
     */
    function student_list() {
        $access = array('student');
        $this->_is_authorize($access); //check super_admin controller

        $this->load->view('group/student_list');
    }
    /**
     * instructor_list_records
     *
     * Load information list for instructor
     *
     * @access public
     * @param void
     * @return void
     */
    function instructor_list() {
        $access = array('instructor');
        $this->_is_authorize($access); //check super_admin controller

        $this->load->view('group/instructor_list');
    }

    function get_group_members() {

        $search = $this->input->post('search', TRUE);
        $group_id = $this->input->post('group_id', TRUE);

        $where = 'ug.group_id = "'.$group_id.'"';

        $join_users = array(
            'table' => USERS . ' u',
            'condition' => 'u.user_id = ug.user_id',
        );

        //Search by
        $filter = 'CONCAT(u.lname, ", ", u.fname) LIKE "%' .$search.'%"';  //instructor

        $where .= ( ! empty($search) ) ? ' AND  '.$filter : NULL;

        $select = 'ug.users_groups_id, CONCAT(u.lname, ", ",u.fname) fullname';

        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        $args = array(
                    'select' => $select,
                    'where' =>  $where,
                    'join' => array($join_users),
                    'order_by' => $order_by,
        );

        //For pagination
        $page = $this->input->post('page');

        $total = count($this->User_group_model->get_list($args));

        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        //PUSH THIS VALUE INTO ARGS
        $args['limit'] = LIMIT;
        $args['offset'] = $offset;
        $args['resource_id'] = TRUE;
        //end Pagination

        $records = $this->User_group_model->get_list($args);

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'users_groups_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }

    function get_student_groups() {

        $search = $this->input->post('search', TRUE);

        $where = 'g.group_id != "!@#"';

        $join_user_groups = array(
            'table' => USERS_GROUPS . ' ug',
            'condition' => 'ug.group_id = g.group_id',
        );

        //Instructor
        $join_u1 = array(
            'table' => USERS . ' u1',
            'condition' => 'u1.user_id = ug.user_id AND u1.user_type = "instructor"',
        );

        //Student
        $join_u2 = array(
            'table' => USERS . ' u2',
            'condition' => 'ug.user_id = "'.  _session('user_id').'"',
            'join' => 'LEFT',
        );

        //Search by
        $filter = '(g.group_name LIKE "' .$search.'%" ';  //group name
        $filter .= 'OR CONCAT(u1.lname, ", ", u1.fname) LIKE "' .$search.'%")';  //instructor

        $where .= ( ! empty($search) ) ? ' AND  '.$filter : NULL;

        $select = 'g.group_id, g.group_name, CONCAT(u1.lname, ", ",u1.fname) instructor, u2.user_id student_id';

        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        $args = array(
                    'select' => $select,
                    'where' =>  $where,
                    'join' => array($join_user_groups, $join_u1, $join_u2),
                    'order_by' => $order_by,
                    'group_by'=> 'group_id',
        );

        //For pagination
        $page = $this->input->post('page');

        $total = count($this->Group_model->get_list($args));

        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        //PUSH THIS VALUE INTO ARGS
        $args['limit'] = LIMIT;
        $args['offset'] = $offset;
        $args['resource_id'] = TRUE;
        //end Pagination

        $records = $this->Group_model->get_list($args);

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'group_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }

    function get_instructor_groups() {

        $search = $this->input->post('search', TRUE);

        $where = 'g.group_id != "!@#"';

        $join_user_groups = array(
            'table' => USERS_GROUPS . ' ug',
            'condition' => 'ug.group_id = g.group_id AND ug.user_id = "'._session('user_id').'"',
        );

        //Instructor
        $join_u1 = array(
            'table' => USERS . ' u1',
            'condition' => 'u1.user_id = "'.  _session('user_id').'" AND u1.user_type = "instructor"',
        );

        //Search by
        $filter = 'g.group_name LIKE "' .$search.'%" ';  //group name

        $where .= ( ! empty($search) ) ? ' AND  '.$filter : NULL;

        $select = 'g.group_id, g.group_name, g.password';

        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        $args = array(
                    'select' => $select,
                    'where' =>  $where,
                    'join' => array($join_user_groups, $join_u1),
                    'order_by' => $order_by,
                    'group_by'=> 'group_id',
        );

        //For pagination
        $page = $this->input->post('page');

        $total = count($this->Group_model->get_list($args));

        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        //PUSH THIS VALUE INTO ARGS
        $args['limit'] = LIMIT;
        $args['offset'] = $offset;
        $args['resource_id'] = TRUE;
        //end Pagination

        $records = $this->Group_model->get_list($args);

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'group_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }
    
    function get_groups() {
        
        $group_name = $this->input->post('group_name', TRUE);
        
        $where = 'g.group_id != "!@#"';
        
        $join_instructor = array(
            'table' => USERS . ' u',
            'condition' => '1 = 1',
            'join' => 'LEFT'
        );
        
        $join_user_groups = array(
            'table' => USERS_GROUPS . ' ug',
            'condition' => 'ug.user_id = u.user_id',
            'join' => 'LEFT'
        );
        

        $where .= ( ! empty($group_name) ) ? ' AND g.group_name LIKE "' .$group_name.'%"' : NULL;

        $select = 'g.group_id, g.group_name, g.password';

        $page = $this->input->post('page');
        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        if ($this->input->post('sidx') == 'category')
            $order_by .= ', sub_category';

        $total = $this->Group_model->get_total($where);
        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        $args = array(
                    'select' => $select,
                    'where' =>  $where,
                    'join' => array($join_instructor, $join_user_groups),
                    'limit' => LIMIT,
                    'offset' => $offset,
                    'order_by' => $order_by,
                    'group_by'=> 'group_id',
                    'resource_id' => TRUE,
        );

        $records = $this->Group_model->get_list($args);
        
        $this->db->group_by('group_id');

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'group_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }

    function enter($group_id = 0) {
        $access = array('student');
        $this->_is_authorize($access); //check super_admin controller

        $where = 'ug.group_id = "'.$group_id.'" AND user_id = "'._session('user_id').'"';
        $data = array(
            'is_joined' => $this->User_group_model->exists($where),
            'group' => $this->Group_model->get_record(array('where' => 'group_id = "'.$group_id.'"'))
        );

        $this->load->view('group/lobby',$data);

    }

    function enter_instructor($group_id = 0) {
        $access = array('instructor');
        $this->_is_authorize($access); //check super_admin controller

        $data = array(
            'group' => $this->Group_model->get_record(array('where' => 'group_id = "'.$group_id.'"'))
        );

        $this->load->view('group/lobby_instructor',$data);

    }

    function check_group() {
        $msg = FALSE;

        $group_id = $this->input->post('group_id', TRUE);
        $password = $this->input->post('password', TRUE);

        $group = $this->Group_model->get_record(array(
            'where' => array(
                'group_id' => $group_id,
                'password' => $password,
            )
        ));

        if (is_array($group)) {
            $msg = TRUE;
            $data = array(
                'user_id' => _session('user_id')
                ,'group_id' => $group_id
            );

            $this->User_group_model->_insert($data);
        }

        echo json_encode($msg);
    }
    
    function add(){
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller

        $data = array(
            'group_id' => 0,
            'group_name' => '',
            'password' => '',
 
        );
        
        $params = array(
            'select' => '*',
            'where' => array('user_type' => 'instructor'),
            'join' => array(),
            'order_by' => NULL
        );

        $data['instructors'] = $this->User_model->get_list($params);
        
        
        $this->load->view('group/form', $data);
        
    }
    
    function edit($id){
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller
        
        $params = array(
                'select' => 'g.*',
                'where' => array('group_id' => $id),
                'join' => array(),
                'order_by' => NULL
        );
        

        $data = $this->Group_model->get_record($params);
        
        $params = array(
            'select' => 'u.*',
            'where' => array('user_type' => 'instructor'),
            'join' => array(),
            'order_by' => NULL
        );

        $data['instructors'] = $this->User_model->get_list($params);
        

        $this->load->view('group/form', $data);
        
        
    }

    function group_setting($id){
        $access = array('instructor');
        $this->_is_authorize($access); //check super_admin controller

        $params = array(
                'select' => 'g.*',
                'where' => array('group_id' => $id),
                'join' => array(),
                'order_by' => NULL
        );


        $data = $this->Group_model->get_record($params);

        $params = array(
            'select' => 'u.*',
            'where' => array('user_type' => 'instructor'),
            'join' => array(),
            'order_by' => NULL
        );

        $data['instructors'] = $this->User_model->get_list($params);
        
        $this->load->view('group/group_setting', $data);
    }
    
    function view($id){
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller

        $join_instructor = array(
            'table' => USERS . ' u',
            'condition' => '1=1',
            'join' => 'LEFT'
        );
        
        $join_user_groups = array(
            'table' => USERS_GROUPS . ' ug',
            'condition' => 'ug.user_id = u.user_id',
            'join' => 'LEFT'
        );
        
        $params = array(
                'select' => 'g.*, u.fname, u.user_id',
                'where' => array('g.group_id' => $id, 'ug.group_id' => $id),
                'join' => array($join_instructor, $join_user_groups),
                'order_by' => NULL
        );

        $data = $this->Group_model->get_record($params);

        $this->load->view('group/display', $data);
        
        
    }
    
    function submit(){
        
        $response = FALSE;
        $group_id = $this->input->post('group_id', TRUE);

        $data = array(
            'group_name' => $this->input->post('group_name',TRUE),
            'password' => $this->input->post('group_password',TRUE),

        );


        if (empty($group_id)) {

            $group_id = $this->Group_model->_insert($data);
            
                    
            $data_group = array(
                'user_id' =>$this->input->post('instructor_id', TRUE),
                'group_id' => $group_id
            );

            $this->User_group_model->_insert($data_group);
            
            $msg = sprintf(lang('cms_add_success'), "Group");
            $response = TRUE;

        } else {
 
            $where = array('group_id' => $group_id);
            
            $data_group = array(
                'user_id' =>$this->input->post('instructor_id', TRUE),
                'group_id' => $group_id
            );

            $this->Group_model->_update($where, $data);
            
            
            $this->User_group_model->_update($where, $data_group);


            $msg = sprintf(lang('cms_edit_success'), "Group");


            $response = TRUE;
        }

        _success($msg, FALSE);
        echo json_encode($response);
    }

    function save_group_setting(){

        $response = FALSE;
        $group_id = $this->input->post('group_id', TRUE);

        $data = array(
            'password' => $this->input->post('group_password',TRUE),
        );

        $where = array('group_id' => $group_id);

        $this->Group_model->_update($where, $data);

        $msg = sprintf(lang('cms_edit_success'), "Group");

        $response = TRUE;

        _success($msg, FALSE);
        echo json_encode($response);
    }
    
    function delete($group_id) {
        _success(sprintf(lang('cms_delete_success'), "Group"));

        $this->Group_model->_delete(array('group_id' => $group_id));

        $msg = sprintf(lang('cms_delete_success'), "Group");

        $response = TRUE;
        _success($msg, FALSE);
        echo json_encode($response);
    }
    
    
    function unique() {
        $group_id = $this->input->get('group_id', TRUE);
        $field = $this->input->get('field', TRUE);

        $where = array(
            $field => $this->input->get($field, TRUE),
        );

        if (!empty($group_id)) {
            $where['group_id !='] = $group_id;
        }

        $valid = ($this->Group_model->exists($where)) ? FALSE : TRUE;
        echo json_encode($valid);
    }
  
    function unique_instructor() {
        $instructor_id = $this->input->get('$instructor_id', TRUE);
        $field = $this->input->get('field', TRUE);

        $where = array(
            $field => $this->input->get($field, TRUE),
        );

        if (!empty($item_id)) {
            $where['item_id !='] = $item_id;
        }

        $valid = ($this->Users_group_model->exists($where)) ? FALSE : TRUE;
        echo json_encode($valid);
    }
  

}
/* End of file cms.php */
/* Location: ./application/controllers/cms.php */
